<template>
  <div>
    <el-form :inline="true" :model="form" 
    size="mini"
    class="getformcs">
  <el-form-item label="姓名">
    <el-input v-model="form.name"></el-input>
  </el-form-item>
    <el-form-item >
      <el-button type="primary" @click="onSubmit">查询</el-button>
</el-form-item>
   <el-form-item >
      <el-button type="primary" @click="onrel">重置</el-button>
</el-form-item>
</el-form>
    <el-table :data="compData" style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center"> </el-table-column>
      <el-table-column prop="sex_text" label="性别" align="center"> </el-table-column>
      <el-table-column prop="age" label="年龄" align="center"> </el-table-column>
      <el-table-column prop="number" label="学号" align="center"> </el-table-column>
      <el-table-column prop="class" label="班级" align="center"> </el-table-column>  
      <el-table-column prop="state_text" label="状态" align="center"> </el-table-column>   
      <el-table-column prop="address" label="地址" align="center"> </el-table-column> 
      <el-table-column prop="phone" label="联系方式" align="center"> </el-table-column>
     <el-table-column label="操作" align="center">
         <template slot-scope="scope">
             <el-button type="danger" size="mini"
              icon="el-icon-delete"
               @click="tapdel(scope.row.id)"> </el-button>
         </template>
     </el-table-column>
     
    </el-table>
        <el-pagination
    @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import { getstudents,deletstudents } from '@/API/api.js';

export default {
  data() {
    return {
        form:{
          name:''
        },
        tableData: [],
      currentPage:1,//当前页数
      pageSize:10,
      total:0,
    };
  },
  created() {
    this.getData();
  },
  computed:{
      compData(){
return this.tableData.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
      }
  },
  methods: {
        handleSizeChange(val) {
            this.pageSize = val
            console.log(val);
            this.currentPage = 1
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
          this.currentPage = val
        console.log(`当前页: ${val}`);
      },
    getData(Params) {
      getstudents(Params).then((res) => {
        console.log(res);
        if(res.status === 200){
            this.tableData = res.data
            this.total = res.total
            this.tableData.forEach(item =>{
                item.sex === 1 ? item.sex_text ='男' : item.sex_text='女'
                item.state ==='1' ? item.state_text ='已入学':item.state=== '2' 
                ? item.state_text ="未入学" :item.state_text ="休学中" 
            })
        }
      });
    },
    // 删除
    tapdel(id){
  deletstudents(id).then(res=>{
    console.log(res);
    if(res.status === 200) {
         this.$message({
          message: '删除数据成功',
          type: 'success'
        });
        this.getData()
    }
  })
    },
    // 查询
    onSubmit(){
      this.getData(this.form)
      console.log(this.form);
    },
    // 重置
    onrel(){
      console.log(this.form);
      this.form = {}
      
      this.getData(this.form)
      console.log(22);
    }
  },
};
</script>
<style scoped lang="scss">
.getformcs{

padding-top: 20px;
}
</style>